package com.eastfair.venueservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venueservice.dao.VenueDrawingCategoryAuditHistoryMapper;
import com.eastfair.venueservice.dto.VenueDrawingCategoryAuditHistoryDTO;
import com.eastfair.venueservice.entity.VenueDrawingCategoryAuditHistory;
import com.eastfair.venueservice.service.VenueDrawingCategoryAuditHistoryService;
import com.eastfair.venueservice.vo.VenueDrawingCategoryAuditHistoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 一键报馆图纸分类审核记录历史
 * </p>
 *
 * @author dq
 * @date 2022-10-31
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class VenueDrawingCategoryAuditHistoryServiceImpl extends SuperServiceImpl<VenueDrawingCategoryAuditHistoryMapper, VenueDrawingCategoryAuditHistory> implements VenueDrawingCategoryAuditHistoryService {


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<VenueDrawingCategoryAuditHistory> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(VenueDrawingCategoryAuditHistory model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public Page<VenueDrawingCategoryAuditHistoryVO> listHistoryVO(PageParams<VenueDrawingCategoryAuditHistoryDTO> pageParams) {
        log.info("listHistoryVO - 查询报馆图纸分类审核历史VO, pageParams={}", pageParams);
        Page<VenueDrawingCategoryAuditHistory> page = listHistory(pageParams);
        Page<VenueDrawingCategoryAuditHistoryVO> voPage = new Page<>();
        BeanUtil.copyProperties(page, voPage);
        List<VenueDrawingCategoryAuditHistory> list = page.getRecords();
        if (list == null || list.isEmpty()) {
            return voPage;
        }
        List<VenueDrawingCategoryAuditHistoryVO> voList = list
                .stream()
                .map(history -> BeanUtil.toBean(history, VenueDrawingCategoryAuditHistoryVO.class))
                .collect(Collectors.toList());
        voPage.setRecords(voList);
        return voPage;
    }

    @Override
    public Page<VenueDrawingCategoryAuditHistory> listHistory(PageParams<VenueDrawingCategoryAuditHistoryDTO> pageParams) {
        Page<VenueDrawingCategoryAuditHistory> pageParam = pageParams.buildPage();
        VenueDrawingCategoryAuditHistoryDTO model = pageParams.getModel();
        QueryWrapper<VenueDrawingCategoryAuditHistory> queryWrapper = createHistoryQueryWrapper(model);
        return page(pageParam, queryWrapper);
    }

    @Override
    public VenueDrawingCategoryAuditHistory saveHistory(VenueDrawingCategoryAuditHistoryDTO historyDTO) {
        log.info("saveHistory - 保存报馆图纸分类审核历史, historyDTO={}", historyDTO);
        VenueDrawingCategoryAuditHistory history = BeanUtil.toBean(historyDTO, VenueDrawingCategoryAuditHistory.class);
        save(history);
        return history;
    }

    @Override
    public boolean saveBatch(List<VenueDrawingCategoryAuditHistoryDTO> list) {
        log.info("saveBatch - 批量保存报馆图纸分类审核历史, list={}", list);
        if (list == null || list.isEmpty()) {
            return true;
        }
        list.forEach(this::saveHistory);
        return true;
    }

    private QueryWrapper<VenueDrawingCategoryAuditHistory> createHistoryQueryWrapper(VenueDrawingCategoryAuditHistoryDTO model) {
        QueryWrapper<VenueDrawingCategoryAuditHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(VenueDrawingCategoryAuditHistory::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(model.getExhibitionManageId() != null, VenueDrawingCategoryAuditHistory::getExhibitionManageId, model.getExhibitionManageId())
                .eq(model.getVenueReportId() != null, VenueDrawingCategoryAuditHistory::getVenueReportId, model.getVenueReportId())
                .eq(StrUtil.isNotBlank(model.getSpaceCode()), VenueDrawingCategoryAuditHistory::getSpaceCode, model.getSpaceCode())
                .eq(model.getDrawingId() != null, VenueDrawingCategoryAuditHistory::getDrawingId, model.getDrawingId())
                .orderByDesc(VenueDrawingCategoryAuditHistory::getAuditTime)
        ;
        return queryWrapper;
    }
}
